﻿using UnityEngine;
using System.Collections;


public delegate void OnStory2dSubTitleBegin();
public delegate void OnStory2dSubTItleEnd();
public class Story2dSubTitle : MonoBehaviour {

    public UILabel label;
    public float TIME_PER_LETTER = 0.1f;

    int currentLabelPos = 0;
    float curTime = 0f;

    public string content;

    bool isPlay = false;

    void Start()
    {
    }

    public void Play(string content)
    {
        this.content = content;
        currentLabelPos = 0;
        curTime = 0;
        isPlay = true;
    }

    public void Over()
    {
        isPlay = false;
        label.text = content;
    }

    void Update()
    {
        if (isPlay)
        {
            curTime += Time.deltaTime;
            if (curTime >= TIME_PER_LETTER)
            {
                if (currentLabelPos < content.Length)
                {
                    label.text = content.Substring(0, currentLabelPos);
                    currentLabelPos++;
                }
                else
                {
                    Over();
                }
                curTime = 0f;
            }
        }

    }
}
